﻿using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastCloud.ORG.Entity;
using FastORM.Entity;
using FastUtil.Convert;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.ORG.DB
{
    public class DB_OU : BaseDBML<Frame_OU>
    {
        public override void Insert(Frame_OU Entity)
        {
            Frame_OU ParentOU = SelectOne(Entity.ParentOUGuid);
            if (string.IsNullOrEmpty(Entity.OUGuid))
            {
                Entity.OUGuid = GenerateUtil.GenerateUnitGuid();
            }
            Entity.OUCode = CloudUtil.CreateTableFieldCodeValue("Frame_OU", "OUGuid", "OUCode", "ParentOUGuid", Entity.ParentOUGuid);
            Entity.AddDate = DateTime.Now;
            Entity.UpdateDate = DateTime.Now;
            Entity.SubOUNum = 0;
            Entity.SubUserNum = 0;
            base.Insert(Entity);
            if (ParentOU != null && !string.IsNullOrEmpty(ParentOU.OUGuid))
            {
                //计算父级部门数量
                List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "ParentOUGuid", ParamValue = Entity.ParentOUGuid } };
                ParentOU.SubOUNum = SelectCount(string.Format("ParentOUGuid=@ParentOUGuid"), ParamList); ;
                base.UpdateByID(ParentOU);
            }


        }

        public override void UpdateByID(Frame_OU Entity)
        {
            Entity.UpdateDate = DateTime.Now;
            Frame_OU DBEntity = SelectOne(Entity.OUGuid);
            if (DBEntity.ParentOUGuid != Entity.ParentOUGuid)
            {
                Entity.OUCode = CloudUtil.CreateTableFieldCodeValue("Frame_OU", "OUGuid", "OUCode", "ParentOUGuid", Entity.ParentOUGuid);
                base.UpdateByID(Entity);

                //计算旧父级部门数量
                Frame_OU OldParentOU = SelectOne(DBEntity.ParentOUGuid);
                if (OldParentOU != null && !string.IsNullOrEmpty(OldParentOU.OUGuid))
                {
                    List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "ParentOUGuid", ParamValue = OldParentOU.OUGuid } };
                    OldParentOU.SubOUNum = SelectCount(string.Format("ParentOUGuid=@ParentOUGuid"), ParamList); ;
                    base.UpdateByID(OldParentOU);
                }

                //计算新父级部门数量
                Frame_OU NewParentOU = SelectOne(Entity.ParentOUGuid);
                if (NewParentOU != null && !string.IsNullOrEmpty(NewParentOU.OUGuid))
                {
                    List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "ParentOUGuid", ParamValue = NewParentOU.OUGuid } };
                    NewParentOU.SubOUNum = SelectCount(string.Format("ParentOUGuid=@ParentOUGuid"), ParamList); ;
                    base.UpdateByID(NewParentOU);
                }
            }
            else
            {
                base.UpdateByID(Entity);
            }
        }

        public override void DeleteByID(string UnitGuid)
        {
            Frame_OU Entity = SelectOne(UnitGuid);
            base.DeleteByID(UnitGuid);

            //计算父级部门数量
            Frame_OU ParentOU = SelectOne(Entity.ParentOUGuid);
            if (ParentOU != null && !string.IsNullOrEmpty(ParentOU.OUGuid))
            {
                List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "ParentOUGuid", ParamValue = ParentOU.OUGuid } };
                ParentOU.SubOUNum = SelectCount(string.Format("ParentOUGuid=@ParentOUGuid"), ParamList);
                base.UpdateByID(ParentOU);
            }
               
        }


        public string GetFullPathOUName(string OUGuid, List<Frame_OU> AllOUList)
        {
            string OUName = string.Empty;
            CaculateSubOuName(AllOUList, OUGuid, ref OUName);
            return OUName;
        }

        private void CaculateSubOuName(List<Frame_OU> AllOUList, string OUGuid, ref string OUName)
        {
            List<Frame_OU> AllOUListFilter = AllOUList.FindAll(a => a.OUGuid == OUGuid);
            if (AllOUListFilter.Count > 0)
            {
                Frame_OU OU = AllOUListFilter[0];
                if (!string.IsNullOrEmpty(OUName))
                {
                    OUName = OU.OUName + " — " + OUName;
                }
                else
                {
                    OUName = OU.OUName;
                }

                CaculateSubOuName(AllOUList, OU.ParentOUGuid, ref OUName);
            }
        }

    }
}
